package com.yx.changdao.web.template;

import com.yx.changdao.common.annotation.AutoLog;
import com.yx.changdao.common.data.Result;
import com.yx.changdao.common.entity.SysUser;
import com.yx.changdao.common.utils.ExportCSVUtils;
import com.yx.changdao.common.utils.ExportUtils;
import com.yx.changdao.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: <br>
 * CSV 文件导出 演示接口
 *
 * <p><br>
 * @Auther: Zhangjiashun
 * @create 2019/11/6 15:58
 */
@Controller
@RequestMapping(value = "/demo/export", produces = "application/shlnd.ms-excel;charset=utf-8")
public class ExportDemoController {
    private final SysUserService sysUserService;

    public ExportDemoController(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @AutoLog
    @GetMapping("/test1")
    @ResponseBody
    public Result list(HttpServletResponse response) {

        List<SysUser> list = this.sysUserService.list();

        if (ExportCSVUtils.export(
                "file",
                response,
                SysUser.class,
                Arrays.asList("用户id", "用户名", "昵称", "头像", "用户未锁定", "创建日期", "创建人", "更新日期", "更新人"),
                list,
                Arrays.asList("password", "salt", "deleteFlag"))) {
            // 处理成功务必返回null
            return null;
        }
        return Result.ok().data(list);
    }

    @AutoLog
    @GetMapping("/test2")
    @ResponseBody
    public Result list2(HttpServletResponse response) {

        List<SysUser> list = this.sysUserService.list();

        if (ExportCSVUtils.export(
                "file",
                response,
                SysUser.class,
                Arrays.asList("用户id", "用户名", "昵称", "头像", "用户未锁定", "创建日期", "创建人", "更新日期", "更新人"),
                list,
                Arrays.asList("password", "salt", "deleteFlag"),
                "yyyy-MM-dd HH:mm:ss")) {
            // 处理成功务必返回null
            return null;
        }
        return Result.ok().data(list);
    }

    @AutoLog
    @GetMapping("/test3")
    @ResponseBody
    public Result list3(HttpServletResponse response) {

        List<SysUser> list = this.sysUserService.list();

        List<Map<String, Object>> dataList = new ArrayList<>();

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        for (SysUser user : list) {
            Map<String, Object> map = new HashMap<>();
            map.put(SysUser.USER_ID, user.getUserId());
            map.put(SysUser.USERNAME, user.getUsername());
            map.put(SysUser.NICKNAME, user.getNickname());
            map.put(SysUser.AVATAR, StringUtils.isBlank(user.getAvatar()) ? "没头像" : user.getAvatar());
            map.put(SysUser.STATUS, user.getStatus() == 1 ? "未锁定" : "锁定");
            map.put(SysUser.CREATE_TIME, sdf1.format(user.getCreateTime()));
            map.put(SysUser.UPDATE_TIME, sdf2.format(user.getUpdateTime()));
            dataList.add(map);
        }

        if (ExportCSVUtils.export(
                "file",
                response,
                Arrays.asList("用户id", "用户名", "昵称", "头像", "用户锁定状态", "创建日期", "更新日期"),
                // 这个顺序影响文件列的输出顺序 和上一参数一一对应。
                Arrays.asList(
                        SysUser.USER_ID,
                        SysUser.USERNAME,
                        SysUser.NICKNAME,
                        SysUser.AVATAR,
                        SysUser.STATUS,
                        SysUser.CREATE_TIME,
                        SysUser.UPDATE_TIME),
                dataList)) {
            return null;
        }
        return Result.ok().data(list);
    }

    @AutoLog
    @GetMapping("/test4")
    @ResponseBody
    public Result list4(HttpServletResponse response) {

        List<SysUser> list = this.sysUserService.list();

        if (ExportUtils.exportCSV(SysUser.class, response, "test", list)) {
            return null;
        }
        return Result.ok().data(list);
    }


    @AutoLog
    @GetMapping("/test5")
    @ResponseBody
    public Result list5(HttpServletResponse response) {

        List<SysUser> list = this.sysUserService.list();

        if (ExportUtils.exportExcel(SysUser.class, response, "test", "用户信息表", list)) {
            return null;
        }
        return Result.ok().data(list);
    }

}
